package com.example.shuiyin.exception;

import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
public class AppException extends RuntimeException {
    
    private final int code;
    private final HttpStatus httpStatus;
    private final ErrorCode errorCode;
    
    public AppException(String message) {
        super(message);
        this.code = ErrorCode.INTERNAL_SERVER_ERROR.getCode();
        this.httpStatus = ErrorCode.INTERNAL_SERVER_ERROR.getHttpStatus();
        this.errorCode = ErrorCode.INTERNAL_SERVER_ERROR;
    }
    
    public AppException(String message, ErrorCode errorCode) {
        super(message);
        this.code = errorCode.getCode();
        this.httpStatus = errorCode.getHttpStatus();
        this.errorCode = errorCode;
    }
    
    public AppException(String message, int code, HttpStatus httpStatus) {
        super(message);
        this.code = code;
        this.httpStatus = httpStatus;
        this.errorCode = null;
    }
    
    public AppException(String message, ErrorCode errorCode, Throwable cause) {
        super(message, cause);
        this.code = errorCode.getCode();
        this.httpStatus = errorCode.getHttpStatus();
        this.errorCode = errorCode;
    }
    
    public AppException(String message, Throwable cause) {
        super(message, cause);
        this.code = ErrorCode.INTERNAL_SERVER_ERROR.getCode();
        this.httpStatus = ErrorCode.INTERNAL_SERVER_ERROR.getHttpStatus();
        this.errorCode = ErrorCode.INTERNAL_SERVER_ERROR;
    }
    
    /**
     * 获取HTTP状态码
     */
    public HttpStatus getHttpStatus() {
        return httpStatus;
    }
    
    /**
     * 获取错误代码
     */
    public int getCode() {
        return code;
    }
    
    /**
     * 获取错误码枚举
     */
    public ErrorCode getErrorCode() {
        return errorCode;
    }
    
    /**
     * 获取HTTP状态码的数值
     */
    public int getStatusCode() {
        return httpStatus != null ? httpStatus.value() : HttpStatus.INTERNAL_SERVER_ERROR.value();
    }
} 